package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车业务层
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    ShoppingCartMapper shoppingCartMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {

        //创建一个shoppingCart对象储存DTO中的一些数据
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //查询购物车表查看是否有数据
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.list(shoppingCart);

        //有数据
        if (shoppingCarts != null && shoppingCarts.size() != 0) {
            shoppingCart = shoppingCarts.get(0);
            Integer number = shoppingCart.getNumber();

            //将数量加一
            number++;
            shoppingCart.setNumber(number);

            //执行修改操作
            shoppingCartMapper.updateNumberById(shoppingCart);

            //返回
            return;
        }

        //没有数据，就判断添加的是套餐还是菜品
        if (shoppingCart.getDishId() != null) {
            //添加的是菜品：根据dishId查询套餐表获得dish对象
            Dish dish = dishMapper.getById(shoppingCart.getDishId());
            //将dish中的部分数据复制到shopCart对象中
            shoppingCart.setName(dish.getName());
            shoppingCart.setAmount(dish.getPrice());
            shoppingCart.setImage(dish.getImage());
        } else {
            //添加的是套餐：根据setmealId查询套餐表获得setmeal对象
            Setmeal setmeal = setmealMapper.getById(shoppingCart.getSetmealId());
            //将setmeal中的部分数据复制到shopCart对象中
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setAmount(setmeal.getPrice());
            shoppingCart.setImage(setmeal.getImage());
        }

        shoppingCart.setNumber(1);
        shoppingCart.setCreateTime(LocalDateTime.now());
        //将数据插入到shoppingCart表中
        shoppingCartMapper.insert(shoppingCart);
    }

    /**
     * 查看购物车
     *
     * @return
     */
    public List<ShoppingCart> showShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(userId)
                .build();

        return shoppingCartMapper.list(shoppingCart);
    }

    /**
     * 清空购物车
     */
    public void cleanShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(userId);
    }

    /**
     * 删除购物车
     * @param shoppingCartDTO
     */
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //获得该购物车数据
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.list(shoppingCart);

        if (shoppingCarts != null && shoppingCarts.size() != 0) {
            shoppingCart = shoppingCarts.get(0);

            //查看数量是否大于一
            Integer number = shoppingCart.getNumber();
            //大于一，将数量减一后update
            if (number > 1) {
                number--;
                shoppingCart.setNumber(number);
                shoppingCartMapper.updateNumberById(shoppingCart);
            } else if (number == 1) {
                //等于一，根据id直接删除
                shoppingCartMapper.deleteById(shoppingCart.getId());
            }
        }
    }







}
